<?php
namespace Common\Service;
use Mobile\Model\tmpmsgModel;
use Mobile\Model\tmplModel;

class MsgService extends BaseService {
    public $tmplModel;
    public $tmpmsgModel;
    public function __construct() {
        $this->tmplModel = new tmplModel();
        $this->tmpmsgModel = new tmpmsgModel();
    }

    public function shouruMsg ($userinfo,$money,$content,$order) {
        $tmp = $this->tmplModel->where(array('tmp_name'=>'FEN_XIAO'))->find();

        $tmp_id = $tmp['tmp_id'];
        $color = $tmp['color'];

        $url = get_domain()."/mobile/moneylists/money/";
        $footer ="资金已转入你的账户".'\\n\\n'."点击查看详情...";
        $msg = array(
            "touser" => $userinfo['openid'],
            "template_id" => $tmp_id,
            'url' => $url,
            "topcolor" => $color,
            'data' => array(
                'first' => array(
                    'value' => urlencode($content.'\\n'),
                    'color' => $color
                ),
                'keyword1' => array(
                    'value' => urlencode($order['order_sn']),//订单号
                    'color' => $color,
                ),
                'keyword2' => array(
                    'value' => urlencode($order['order_cashmoney']),//订单金额
                    'color' => $color,
                ),
                'keyword3' => array(
                    'value' => urlencode($money),//订单金额
                    'color' => $color,
                ),
                'keyword4' => array(
                    'value' => urlencode($order['pay_time']),
                    'color' => $color,
                ),
                'remark' => array(
                    'value' => urlencode($footer),
                    'color' => $color,
                ),
            ),
        );
        $msgttt = array(
            "uid" => $userinfo['uid'],
            "openid" => $userinfo['openid'],
            "nickname" => $userinfo['nickname'],
            "groupid" => -1,
            "content" => urldecode(json_encode($msg)),
            "send_priority" => 1,
            "create_time" => time(),
            "send_time" => time(),
            "status" => -1,
            "send_plan_time" => time()
        );
        $this->tmpmsgModel->addtmpmsg($msgttt);//添加消息到数据库队列
    }

    public function newOrder ($userinfo,$order) {
        $tmp = $this->tmplModel->where(array('tmp_name'=>'KH_MSG'))->find();

        $orderMember = M('member')->where(array('uid'=>$order['uid']))->field('nickname,mobile')->find();
        $delivery = unserialize($order['delivery']);

        $tmp_id = $tmp['tmp_id'];
        $color = $tmp['color'];
        $content = "恭喜您,您的团队{$orderMember['nickname']}购买了产品，商品名称是：{$order['order_title']}";
        $url = get_domain()."/mobile/index/";
        $footer ="";
     
        $msg = array(
            "touser" => $userinfo['openid'],
            "template_id" => $tmp_id,
            'url' => $url,
            "topcolor" => $color,
            'data' => array(
                'first' => array(
                    'value' => urlencode($content.'\\n'),
                    'color' => $color
                ),
                'keyword1' => array(
                    'value' => urlencode($order['delivery_name']?$order['delivery_name']:$orderMember['nickname']),
                    'color' => $color,
                ),
                'keyword2' => array(
                    'value' => urlencode($order['delivery_mobile']?$order['delivery_mobile']:$orderMember['mobile']),
                    'color' => $color,
                ),
                'keyword3' => array(
                    'value' => urlencode($order['create_time']),
                    'color' => $color,
                ),
                'remark' => array(
                    'value' => urlencode($footer),
                    'color' => $color,
                ),
            ),
        );
        $msgttt = array(
            "uid" => $userinfo['uid'],
            "openid" => $userinfo['openid'],
            "nickname" => $userinfo['nickname'],
            "groupid" => -1,
            "content" => urldecode(json_encode($msg)),
            "send_priority" => 1,
            "create_time" => time(),
            "send_time" => time(),
            "status" => -1,
            "send_plan_time" => time()
        );
        $this->tmpmsgModel->addtmpmsg($msgttt);//添加消息到数据库队列
    }
}